using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using MT.eVotePRS.Common;
using MT.eVotePRS.Model;

namespace MT.eVotePRS.UI
{
    public partial class frmLost : Form
    {
        StudentInfo studentInfo;
        InventoryTxn inventoryTxn;

        public frmLost()
        {
            InitializeComponent();
            timer1.Start();

            studentInfo = new StudentInfo();
            inventoryTxn = new InventoryTxn();
        }

        private void frmLost_Load(object sender, EventArgs e)
        {
            txtDatetime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            txtDatetime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        }
    
        private void btnClose_Click(object sender, EventArgs e)
        {
            if (txtBarcode.Text != string.Empty || txtStudentName.Text != string.Empty || txtStudentID.Text != string.Empty
                || txtHandsetID.Text != string.Empty || txtRemark.Text != string.Empty || txtPayNo.Text != string.Empty )
            {
                if (DialogBox.boxConfirmNo("Are you sure to abort the current action?") == DialogResult.No)
                    return;
            }
            this.Close();
            this.Dispose();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (inventoryTxn != null && inventoryTxn.Id > 0)
            {
                if (!IsValidate())
                    return;
                try
                {
                    InventoryTxn inventTxn = inventoryTxn;

                    inventTxn.Id = 0;
                    inventTxn.TxnTime = DateTime.Now;
                    inventTxn.Status = Presentation.Util.Constants.Status_Fault.Id;
                    inventTxn.UserEvent = Presentation.Util.Constants.Status_Fault.StatusDesc;
                    inventTxn.PaymentNo = txtPayNo.Text;
                    inventTxn.NeedPay = true;
                    inventTxn.Remark = txtRemark.Text;
                    inventTxn.IsOverride = false;
                    inventTxn.OverrideStatus = Presentation.Util.Constants.Status_Nil.Id;

                    object objTxnId = new Presentation.Logic.InventoryTxnActions().CreateNewTxn(inventTxn);

                    if (objTxnId != null && Convert.ToInt32(objTxnId) > 1)
                    {
                        DialogBox.boxInfo("Completed !");

                        studentInfo = new StudentInfo();
                        inventoryTxn = new InventoryTxn();

                        txtHandsetID.Text = "";
                        txtRemark.Text = "";
                        txtPayNo.Text = "";
                        txtStudentName.Text = "";
                        txtStudentID.Text = "";
                        txtBarcode.Text = "";
                    }
                    else
                    {
                        DialogBox.boxWarning("Updated failed!", "Warning!");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    txtBarcode.Focus();
                    txtBarcode.SelectAll();
                }
            }
        }

        private bool IsValidate()
        {
            bool ret = true;
            // student id
            if ( string.IsNullOrEmpty( txtStudentID.Text.Trim()))
            {
                DialogBox.boxWarning("Invalid Student ID!", "Warning!");
                txtStudentID.Select();                                
                return false;
            }
            // keypad id
            if (string.IsNullOrEmpty(txtHandsetID.Text.Trim()))
            {
                DialogBox.boxWarning("Invalid Handset ID!", "Warning!");
                txtHandsetID.Select();
                return false;
            }
            // keypad id
            if ( string.IsNullOrEmpty(txtPayNo.Text.Trim() ))
            {
                DialogBox.boxWarning("Invalid Payment Note No.!", "Warning!");
                txtPayNo.Select();                                
                return false;
            }

            return ret;
        }

        private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                studentInfo.Barcode = txtBarcode.Text.Trim();
                studentInfo.StudentNo = txtStudentID.Text.Trim();

                if (string.IsNullOrEmpty(studentInfo.Barcode) || !Common.Helper.Helper.IsNumeric(studentInfo.Barcode))
                {
                    DialogBox.boxWarning("Invalid Barcode ID!", "Warning!");
                    txtBarcode.SelectAll();
                    return;
                }

                txtStudentID.Text = "";
                txtStudentName.Text = "";
                txtHandsetID.Text = "";
                
                studentInfo = new Presentation.Logic.StudentActions().FindStudentByBarcode(studentInfo.Barcode);
                if (studentInfo == null || studentInfo.Id < 1)
                {
                    DialogBox.boxWarning("The student is invalided in this system!", "Warning!");
                    studentInfo = new StudentInfo();
                }
                else 
                {
                    txtStudentID.Text = studentInfo.StudentNo;
                    txtStudentName.Text = studentInfo.StudentName;
                    
                    checkStudentReportLost(studentInfo.StudentNo);
                }
            }
        }
        
        private void txtStudentID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                studentInfo.StudentNo = txtStudentID.Text.Trim();

                if (string.IsNullOrEmpty(studentInfo.StudentNo) || Common.Helper.Helper.IsNumeric(studentInfo.StudentNo))
                {
                    DialogBox.boxWarning("Invalid Student ID!", "Warning!");
                    txtStudentID.SelectAll();
                    return;
                }

                txtStudentID.Text = "";
                txtStudentName.Text = "";
                txtHandsetID.Text = "";

                studentInfo = new Presentation.Logic.StudentActions().FindStudentByNo(studentInfo.StudentNo);
                if (studentInfo == null || studentInfo.Id < 1)
                {
                    DialogBox.boxWarning("The student is invalided in this system!", "Warning!");
                    studentInfo = new StudentInfo();
                }
                else
                {
                    txtStudentID.Text = studentInfo.StudentNo;
                    txtStudentName.Text = studentInfo.StudentName;

                    checkStudentReportLost(studentInfo.StudentNo);
                }
            }
        }

        void checkStudentReportLost(string studentNo)
        {
            InventoryTxn invTxn = new Presentation.Logic.InventoryTxnActions().CheckStudentStatus(studentNo);
            if (invTxn != null)
            {
                if (invTxn.Status != Presentation.Util.Constants.Status_CheckOut.Id)
                {
                    // DialogBox.boxWarning("The student can not report faulty!", "Warning!");
                    DialogBox.boxWarning(string.Format("This Student '{0}' have not Checked-out a handset!", studentNo), "Warning!");

                    btnConfirm.Enabled = false;
                    
                    txtRemark.ReadOnly = true;
                    txtPayNo.ReadOnly = true;

                    txtHandsetID.Text = "";
                    txtStudentID.SelectAll();
                    
                    inventoryTxn = null;
                }
                else
                {
                    inventoryTxn = invTxn;
                    txtHandsetID.Text = inventoryTxn.HandsetID;
                }
            }
        }
    }
}